﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class TACGIADAO
    {

        public string laytentacgia(int matacgia)
        {
            string ten = "";
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "SELECT TenTacGia FROM TAC_GIA WHERE MaTacGia = '" + matacgia + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            object temp = cmd.ExecuteScalar();
            ten = temp.ToString();
            conn.Close();
            conn.Dispose();
            return ten;
        }

        public int laymatacgia(string tentacgia)
        {
            int kq = 0;
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "SELECT MaTacGia FROM TAC_GIA WHERE TenTacGia = N'"+ tentacgia +"'";
            SqlCommand cmd = new SqlCommand(sql,conn);
            object temp = cmd.ExecuteScalar();
            kq = Convert.ToInt32(temp);
            conn.Close();
            conn.Dispose();
            return kq;
        }

        public bool kiemtratentacgia(string tentacgia)
        {
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "SELECT * FROM TAC_Gia WHERE TenTacGia = N'" + tentacgia + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
            if (kq > 0)
                return true;
            return false;
        }

        public bool Themtacgia(string tentacgia)
        {
            if (kiemtratentacgia(tentacgia) == false)
            {
                SqlConnection conn = DATAPROVIDER.ConnectSQL();
                string sql = "INSERT INTO TAC_GIA(TenTacGia) VALUES(@ttg)";
                SqlCommand cmd = new SqlCommand(sql, conn);

                SqlParameter sqlparameter = new SqlParameter("@ttg", System.Data.SqlDbType.NVarChar, 50);
                sqlparameter.Value = tentacgia;
                cmd.Parameters.Add(sqlparameter);

                conn.Close();
                conn.Dispose();
                int kq = cmd.ExecuteNonQuery();
                if (kq > 0)
                    return true;
                return false;
            }
            else
                return false;
        }

        public DataTable Loadtacgia()
        {
            DataTable datatble = new DataTable();
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "SELECT * FROM TAC_GIA";
            SqlDataAdapter sqldataadapter = new SqlDataAdapter(sql, conn);
            sqldataadapter.Fill(datatble);
            conn.Close();
            conn.Dispose();
            sqldataadapter.Dispose();
            return datatble;
        }
    }
}
